C code for getting and setting sort fields

The Crystal Report Engine includes two functions for getting information from a report about sort fields, PEGetNSortFields and PEGetNthSortField, as well as a third function for changing information in an existing sort field or adding a new sort field to the report, PESetNthSortField. The following sample code demonstrates techniques for using each of these three functions:

short            nSortFields;
    // Will receive number of sort fields
short        sortFieldN;
    // Indicates specific sort field
HANDLE nameHandle;
    // Pointer to name of sort field
short         nameLength;
    // Length of sort field name
short         direction;
    // Sort direction
char     *sortFieldName
    // Holds actual sort field name
short         newDirection
    // New sort field direction
char      newSortName [] = "{orders.ORDERNUM}";
    // New sort field name
nSortFields = PEGetNSortFields(Job);
if (nSortFields < 0){
    // Handle error
}
sortFieldN = 0;
    // Get information for the first sort
    // field in the report
if (!PEGetNthSortField(Job, sortFieldN, &nameHandle,
&nameLength, &direction)){
    // Handle error
}
    // nameLength now contains the length of the string in nameHandle
    // direction now contains either
    // PE_SF_ASCENDING or PE_SF_DESCENDING
    // allocate sortFieldName buffer
sortFieldName = (char*)malloc(nameLength);
    // Retrieve the sort field name
if (!PEGetHandleString(nameHandle, sortFieldName, nameLength)){
    // Handle error
}
    // sortFieldName now contains the sort field
    // Now we will add a new sort field
newDirection = PE_SF_ASCENDING     // By using the value returned by PEGetNSortFields in
    // PESetNthSortField, we add a new sort field to the report:
if (!PESetNthSortField(Job, nSortFields, newSortName,
newDirection))
{
    // Handle error
}


Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com